﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.Entities;
using Iacym.BusinessLogic;
using Microsoft.Practices.Unity;

namespace Iacym.UI
{
    public partial class FrmMttoMinisterio : Form
    {
        [Dependency]
        public IMinisterioBL MinisterioBL { get; set; }
        bool isNew = true;
        public FrmMttoMinisterio()
        {
            InitializeComponent();
            this.BuildUp();
        }
        public void AddMinisteriio()
        {
            mINISTERIOBindingSource.AddNew();
        }
        public MINISTERIO _ministerio { get; set; }
        public void UpdateMinisterio()
        {
            isNew = false;
            
            mINISTERIOBindingSource.DataSource = _ministerio;

        }
        private void FormMttoMinisterio_Load(object sender, EventArgs e)
        {

        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var ministerio = mINISTERIOBindingSource.Current as MINISTERIO;
            
            var msg = "ERROR";
            if (ministerio != null)
            {
                
                if (isNew)
                {
                    try
                    {
                        ministerio.Descripcion = descripcionTextBox.Text.ToUpper();
                        MinisterioBL.AddMinisterio(ministerio);
                        isNew = false;
                        MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    try
                    {
                        ministerio.Descripcion = descripcionTextBox.Text.ToUpper();
                        MinisterioBL.UpdateMinisterio(ministerio);
                        isNew = false;
                        UpdateMinisterio();
                        MessageBox.Show("Modificación Correcta", "Mensaje", 
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();

                    }
                    catch (Exception ex)
                    {

                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }

        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}

